package com.hdoit.kylin.logical.money.dao.impl;
import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.kylin.logical.baseInfo.model.MoneyType;
import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.group.model.Project;
import com.hdoit.kylin.logical.money.dao.IReimbursementDao;
import com.hdoit.kylin.logical.money.model.Reimbursement;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.viewmodel.money.ReimbursementSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;

/**
* @author luozhicheng
* @email
* @createtime 2017年07月18日 19:48:15
* @comment
*/
@Component
public class ReimbursementDaoImpl extends SuperDao<Reimbursement> implements IReimbursementDao{
	
	@Override
	public List<Reimbursement> queryStudentList(ReimbursementSearch search,PagerModel pagerModel){
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("money.*,project.projectName as theProjectName,student.studentName as theUserName,group_info.groupName as theGroupName,mT.moneyType as theMoneyTypeName")
									 .from(Reimbursement.class,"money")
									 .leftJoin(Student.class,"student")
									 .on("money.employeeId","student.employeeId")
									 .andWhere("student.employeeId IS NOT NULL")
									 .leftJoin(Project.class,"project")
									 .on("money.projectId","project.projectId")
									 .leftJoin(Group.class,"group_info")
									 .on("money.groupId","group_info.groupId")
									 .leftJoin(MoneyType.class,"mT")
									 .on("money.moneyTypeId","mT.id")
									 .setMainTableAlias("money");
			
		appendFinder(finderExpression,search);
		return queryList(finderExpression,search,pagerModel);
	}
	
	@Override
	public List<Reimbursement> queryTeacherList(ReimbursementSearch search,PagerModel pagerModel){
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("money.*,project.projectName as theProjectName,teacher.teacherName as theUserName,group_info.groupName as theGroupName")
							 		 .from(Reimbursement.class,"money")
							 		 .leftJoin(Teacher.class,"teacher")
							 		 .on("money.employeeId","teacher.employeeId")
							 		 .andWhere("teacher.employeeId IS NOT NULL")
							 		 .leftJoin(Project.class,"project")
							 		 .on("money.projectId","project.projectId")
							 		 .leftJoin(Group.class,"group_info")
							 		 .on("money.groupId","group_info.groupId")
							 		 .setMainTableAlias("money");
		
		appendFinder(finderExpression,search);
		return queryList(finderExpression,search,pagerModel);
	}

	
	private void appendFinder(IFinderExpression finderExpression,ReimbursementSearch search){
		if(search.getProjectIds()!=null&&search.getProjectIds().size()>0){
			finderExpression.andWhereInString("money.projectId", search.getProjectIds());
		}

			
	}

	@Override
	public int queryStudentCount(ReimbursementSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT money.id) as count")
									 .from(Reimbursement.class,"money")
									 .leftJoin(Student.class,"student")
									 .on("money.employeeId","student.employeeId")
									  .andWhere("student.employeeId IS NOT NULL")
									 .leftJoin(Project.class,"project")
									 .on("money.projectId","project.projectId")
									 .setMainTableAlias("money");
		appendFinder(finderExpression,search);
		return queryCount(finderExpression,search);
	}
	
	@Override
	public int queryTeacherCount(ReimbursementSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT money.id) as count")
									 .from(Reimbursement.class,"money")
									 .leftJoin(Teacher.class,"teacher")
									 .on("money.employeeId","teacher.employeeId")
									  .andWhere("teacher.employeeId IS NOT NULL")
									 .leftJoin(Project.class,"project")
									 .on("money.projectId","project.projectId")
									 .setMainTableAlias("money");
		appendFinder(finderExpression,search);
		return queryCount(finderExpression,search);
	}


}